package lnp;

import static org.junit.Assert.assertTrue;
import lnp.test.RobotTestBed;
import robocode.control.events.RoundEndedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;


/**
 * Illustrates JUnit testing of Robocode robots.
 * This test simply verifies that the bullet hit the target 
 * at least 7 times each round.
 * 
 * @author Lyneth Peou
 *
 */
public class TestFiringDockBulletHitEnemy extends RobotTestBed {
  /** True if the bullet hit enemy at least 7 times. */
  Boolean bulletHitEnemy = false; 
  /** Counter for number of bullet hits enemy. */
  int totalHitEnemy; 
  /** Initialize the current and previous energy. */
  double previousEnergy = 0.0;
  double currentEnergy = 0.0;   

  /**
   * Specifies that Crazy and FiringDock are to be matched up in this test case.
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.Corners,lnp.FiringDock";
  }
  
  /**
   * This test runs for 10 rounds.
   * @return The number of rounds. 
   */
  @Override
  public int getNumRounds() {
    return 10;
  } 
  
  /**
   * Assuming that the bullet hits enemy if the robot gain energy.
   * 
   *  @param e details the TurnEndedEvent
   */
  @Override
  public void onTurnEnded(TurnEndedEvent e) {
    int turn = e.getTurnSnapshot().getTurn();
    IRobotSnapshot robot = e.getTurnSnapshot().getRobots()[1];    
    
    //Set initial energy
    if (turn == 0) {
      currentEnergy = robot.getEnergy();
    }
    else {
      previousEnergy = currentEnergy;
      currentEnergy = robot.getEnergy();
    }   
    
    if (currentEnergy > previousEnergy) {
      totalHitEnemy++; 
    }
  }
  
  /**
   * Asserts that the bullet hits enemy at least seven times.
   * 
   * @param e Details about the completed battle.
   */
  public void onRoundEnded(RoundEndedEvent e) {
    if (totalHitEnemy > 7 && totalHitEnemy < 15) {
      bulletHitEnemy = true;
    }
    assertTrue("Check FiringDock firing target", bulletHitEnemy );    
  }  
  
}
